﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Maticsoft.Common;

namespace Maticsoft.Web.Admin.Shopping
{
    public partial class BookPics : PageBase
    {
        BLL.BookPics bll = new BLL.BookPics();
        int _BookId;
        protected void Page_Load(object sender, EventArgs e)
        {
            Check("shopping", "modify");

            int.TryParse(Request.QueryString["bookId"], out _BookId);
            BindData();

            if (Request.QueryString["act"] == "edit" && !string.IsNullOrEmpty(Request.QueryString["bookPicId"]))
            {
                Edit();
            }
        }

        protected void BindData()
        {
            string strWhere = "bp.IsActive=1 and bp.BookId=" + _BookId;
            DataSet ds = bll.GetListByBookId(strWhere);
            Repeater1.DataSource = ds.Tables[0].DefaultView;
            Repeater1.DataBind();
        }

        protected void Edit()
        {
            Model.BookPics model = bll.GetModel(int.Parse(Request.QueryString["bookPicId"]));
            Image1.ImageUrl = "~/uploadpic/small/" + model.Pic;
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            if (Request.QueryString["act"] == "edit" && !string.IsNullOrEmpty(Request.QueryString["bookPicId"]))
            {
                Model.BookPics model = bll.GetModel(int.Parse(Request.QueryString["bookPicId"]));
                ImageUpload img = new ImageUpload();
                img.SavePath = "~/uploadpic/small/";
                if (File1.Value != "")
                {
                    img.FormFile = File1;
                    img.Upload();
                    if (img.Error == 0)
                    {
                        model.Pic = img.OutFileName;
                    }
                }
                model.BookId = _BookId;
                model.IsActive = 1;
                bll.Update(model);
            }
            else if (Request.QueryString["act"] == "add")
            {
                Model.BookPics model = new Model.BookPics();
                ImageUpload img = new ImageUpload();
                img.SavePath = "~/uploadpic/small/";
                img.FormFile = File1;
                img.Upload();
                if (img.Error == 0)
                {
                    model.Pic = img.OutFileName;
                }
                model.BookId = _BookId;
                model.IsActive = 1;
                bll.Add(model);
            }

            Response.Redirect("BookPics.aspx?act=add&bookId=" + _BookId);
        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            if (txtIds.Value.Trim() != "")
            {
                foreach (string s in txtIds.Value.Trim().Split(','))
                {
                    bll.Delete(int.Parse(s));
                }
            }

            Response.Redirect("BookPics.aspx?act=add&bookId=" + _BookId);
        }
    }
}